package com.dfiles.provider;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dfiles.Config;
import com.dfiles.R;
import com.dfiles.bean.DFile;
import com.dfiles.listener.IconLoadFinishListener;
import com.dfiles.provider.FileTypeHelper.FileTypeAll;

public class FileIconHelper implements IconLoadFinishListener {
	private Context context;
	private FileIconLoader mIconLoader;

	public FileIconHelper(Context context) {
		this.context = context;
		mIconLoader = new FileIconLoader(context, this);
	}

	/* 文件类型集合 */
	public static HashMap<FileTypeAll, String[]> fileExtTypes = new HashMap<FileTypeAll, String[]>();
	static {
		fileExtTypes.put(FileTypeAll.FILE_TYPE_IMAGE, new String[]{"jpg", "jpeg", "gif", "png", "bmp", "wbmp"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_AUDIO, new String[]{"mp3", "ogg", "wma", "wav", "mid", "flac", "m4a"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_VIDEO, new String[]{"mp4", "wmv", "mpeg", "m4v", "3gp", "3gpp", "3g2", "3gpp2", "asf", "rmvb"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_PPT, new String[]{"ppt", "pptx"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_WORD, new String[]{"doc", "docx"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_EXCEL, new String[]{"xls", "xlsx"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_APK, new String[]{"apk"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_PDF, new String[]{"pdf"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_WEB, new String[]{"htm", "html"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_TEXT, new String[]{"txt", "log", "xml", "ini", "lrc"});
		fileExtTypes.put(FileTypeAll.FILE_TYPE_PACKAGE, new String[]{"rar", "zip", "7z"});
	}

	private static HashMap<String, Integer> fileExtToIcons = new HashMap<String, Integer>();

	static {
		addItem(new String[]{"mp3"}, R.drawable.d_file_mp3);
		// addItem(new String[]{"wma"}, R.drawable.file_icon_wma);
		// addItem(new String[]{"wav"}, R.drawable.file_icon_wav);
		// addItem(new String[]{"mid"}, R.drawable.file_icon_mid);
		addItem(new String[]{"mp4", "wmv", "mpeg", "m4v", "3gp", "3gpp", "3g2", "3gpp2", "asf", "rmvb"}, R.drawable.d_file_mp4);
		addItem(new String[]{"jpg", "jpeg", "gif", "png", "bmp", "wbmp"}, R.drawable.d_file_jpg);
		addItem(new String[]{"txt", "log", "xml", "ini", "lrc"}, R.drawable.d_file_txt);
		addItem(new String[]{"doc", "ppt", "docx", "pptx", "xls", "xlsx"}, R.drawable.d_file_doc);
		addItem(new String[]{"pdf"}, R.drawable.d_file_pdf);
		addItem(new String[]{"zip"}, R.drawable.d_file_app);
		addItem(new String[]{"rar", "7z", "zip"}, R.drawable.d_file_rar);
		addItem(new String[]{"htm", "html"}, R.drawable.d_file_html);
	}

	private static void addItem(String[] exts, int resId) {
		if (exts != null) {
			for (String ext : exts) {
				fileExtToIcons.put(ext.toLowerCase(), resId);
			}
		}
	}
	public static int getFileIcon(String ext) {
		Integer i = fileExtToIcons.get(ext.toLowerCase());
		if (i != null) {
			return i.intValue();
		} else {
			return R.drawable.file_icon_default;
		}

	}

	/**
	 * 根据后缀名获取大类型
	 * 
	 * @param suffix
	 * @return
	 */
	public static FileTypeAll getFileTypeBySuffix(String filePath) {
		String extFromFilename = Util.getExtFromFilename(filePath);
		String[] tmps = null;
		Iterator it = fileExtTypes.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Entry) it.next();
			tmps = (String[]) entry.getValue();
			if (tmps != null) {
				for (int i = 0; i < tmps.length; i++) {
					if (tmps[i].equalsIgnoreCase(extFromFilename)) {
						return (FileTypeAll) entry.getKey();
					}
				}
			}
		}
		return FileTypeAll.FILE_TYPE_OTHER;
	}

	/* 特定文件名后面的背景色 */
	private static HashMap<String, String> fileExtToBg = new HashMap<String, String>();
	static {
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_FOLDER), "#eeeeee");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_IMAGE), "#ffffff");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_AUDIO), "#f69988");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_VIDEO), "#002d8a");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_PPT), "#673ab7");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_WORD), "#c5cae9");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_EXCEL), "#c5cae9");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_APK), "#019eaf");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_PDF), "#be1d49");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_WEB), "#e39f18");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_TEXT), "#c5cae9");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_PACKAGE), "#6889ff");
		addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_OTHER), "#eeeeee");
	}

	/**
	 * 不同文件类型有不同的背景
	 * 
	 * @param exts
	 * @param resId
	 */
	private static void addItemBg(String[] exts, String color) {
		if (exts != null) {
			for (String ext : exts) {
				fileExtToBg.put(ext.toLowerCase(), color);
			}
		}
	}

	/**
	 * 
	 * @Description: 文件图标
	 * @param exts
	 * @param resId
	 * @return void
	 * @throw
	 */
	private static void addFileIcon(String[] exts, int resId) {
		if (exts != null) {
			for (String ext : exts) {
				fileExtToIcons.put(ext.toLowerCase(), resId);
			}
		}
	}

	/**
	 * 获取各类型文件的背景图
	 * 
	 * @param theme
	 * @param ext
	 * @return
	 */
	public static int getFileIconBg(int theme, String ext) {
		Integer i = null;
		String color = fileExtToBg.get(ext.toLowerCase());
		if (color == null || color.equals("")) {
			color = "#999999";// 文件夹和未知文件
		}
		i = Color.parseColor(color);
		return i;

	}

	/**
	 * 获取各类型文件的大图标
	 * 
	 * @param theme
	 * @param ext
	 * @return
	 */
	public static int getFileIcon(int theme, String ext) {
		Integer i = null;
		i = fileExtToIcons.get(ext.toLowerCase());
		if (i != null) {
			return i.intValue();
		} else {
			return 0;
		}
	}

	public void setIcon(DFile fileInfo, int flagViewType, TextView textView, ImageView thumbnailInner, ImageView thumbnail) {
		String filePath = fileInfo.path;
		long fileId = fileInfo.dbId;
		String extFromFilename = Util.getExtFromFilename(filePath);
		int idBig = 0;
		if (fileInfo.isFolder) {
			idBig = R.drawable.folder_close;
			thumbnailInner.setImageResource(idBig);
		} else {
			idBig = getFileIcon(flagViewType, extFromFilename);
			thumbnailInner.setImageResource(idBig);
		}
		thumbnail.setBackgroundColor(getFileIconBg(flagViewType, extFromFilename));
		mIconLoader.cancelRequest(thumbnail);
		boolean set = false;
		FileTypeAll fc = FileIconHelper.getFileTypeBySuffix(filePath);
		switch (fc) {
			case FILE_TYPE_APK :
				set = mIconLoader.loadIcon(thumbnailInner, filePath, fileId, fc);
				break;
			case FILE_TYPE_IMAGE :
			case FILE_TYPE_VIDEO :
				thumbnailInner.setVisibility(View.GONE);
				set = mIconLoader.loadIcon(thumbnail, filePath, fileId, fc);
				if (set) {
				} else {
					int defaultImageIcon = (flagViewType == Config.UI.FLAG_VIEW_GRID) ? R.drawable.d_file_jpg : R.drawable.d_file_jpg;
					int defaultVedioIcon = (flagViewType == Config.UI.FLAG_VIEW_GRID) ? R.drawable.d_file_mp4 : R.drawable.d_file_mp4;
					thumbnail.setImageResource(fc == FileTypeAll.FILE_TYPE_IMAGE ? defaultImageIcon : defaultVedioIcon);
					set = true;
				}
				break;
			default :
				set = true;
				break;
		}
	}

	@Override
	public void onIconLoadFinished(ImageView view) {
	}
}
